<template>
    <div id="temp">
        <div class="stuInfo">
            <div class="tit">
                <span>运输设备详情</span>
                <span class="icon" @click="editTransport()"></span>
            </div>
            <el-row>
                <el-col :span="7">
                    <div>
                        <el-col :span="10">
                            <span class="left">车牌号：</span>
                        </el-col>
                        <el-col :span="14">
                            <span class="right">{{transport.number}}</span>
                        </el-col>
                    </div>
                    <div>
                        <el-col :span="10">
                            <span class="left">购车时间：</span>
                        </el-col>
                        <el-col :span="14">
                            <span class="right">{{transport.buy_time}}</span>
                        </el-col>
                    </div>
                </el-col>
                <el-col :span="7">
                    <div>
                        <el-col :span="10">
                            <span class="left">资产编号：</span>
                        </el-col>
                        <el-col :span="14">
                            <span class="right">{{transport.asset_num}}</span>
                        </el-col>
                    </div>
                    <div>
                        <el-col :span="10">
                            <span class="left">资产名称：</span>
                        </el-col>
                        <el-col :span="14">
                            <span class="right">{{transport.asset_name}}</span>
                        </el-col>
                    </div>

                </el-col>
                <el-col :span="7">
                    <div>
                        <el-col :span="10">
                            <span class="left">品牌：</span>
                        </el-col>
                        <el-col :span="14">
                            <span class="right">{{transport.trademark}}</span>
                        </el-col>
                    </div>
                    <div>
                        <el-col :span="10">
                            <span class="left">价值：</span>
                        </el-col>
                        <el-col :span="14">
                            <span class="right">{{transport.value}}</span>
                        </el-col>
                    </div>
                </el-col>
            </el-row>
        </div>
        <div class="record">
            <el-tabs type="border-card" @tab-click="formChange" activeName="use">
                <el-tab-pane label="用车记录" name="use">
                    <table class="tab-table">
                        <thead>
                        <tr>
                            <th width="10%">
                                <el-button type="text" @click="openAdd('use')"><span class="icon add"></span>
                                </el-button>
                            </th>
                            <th width="5%">序号</th>
                            <th>用车人</th>
                            <th>使用日期</th>
                            <th>归还日期</th>
                            <th>使用目的</th>
                            <th>公里数</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr v-for="(item,index) in list" :key="item.id">
                            <td>
                                <span class="icon edit" @click="edit(item.id,'use')"></span>
                                <span class="icon del" @click="Del(item.id,'use')"></span>
                            </td>
                            <td>{{index + 1}}</td>
                            <td>{{item.name}}</td>
                            <td>{{item.use_date}}</td>
                            <td>{{item.return_date}}</td>
                            <td>{{item.purpose}}</td>
                            <td>{{item.kilometres}}</td>
                        </tr>
                        </tbody>
                    </table>
                </el-tab-pane>
                <el-tab-pane label="维修保养记录" name="maintenance">
                    <el-tabs v-model="activeName" @tab-click="handleClick">
                        <el-tab-pane label="维修记录" name="maintenance">
                            <table class="tab-table">
                                <thead>
                                <tr>
                                    <th>
                                        <el-button type="text" @click="openAdd('maintenance')"><span
                                                class="icon add"></span>
                                        </el-button>
                                    </th>
                                    <th>序号</th>
                                    <th>资产编号</th>
                                    <th>资产名称</th>
                                    <th>故障发生时间</th>
                                    <th>故障现象</th>
                                    <th>维修状态</th>
                                    <th>故障解除时间</th>
                                    <th>金额</th>
                                    <th>负责人</th>
                                    <th>备注</th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr v-for="(item,index) in list" :key="item.id">
                                    <td>
                                        <span class="icon edit" @click="edit(item.id,'maintenance')"></span>
                                        <span class="icon del" @click="Del(item.id,'maintenance')"></span>
                                    </td>
                                    <td>{{index + 1}}</td>
                                    <td>{{item.asset_num}}</td>
                                    <td>{{item.asset_name}}</td>
                                    <td>{{item.start_time}}</td>
                                    <td>{{item.phenomenon}}</td>
                                    <td>{{item.condition}}</td>
                                    <td>{{item.end_time}}</td>
                                    <td>{{item.money}}</td>
                                    <td>{{item.leading_official}}</td>
                                    <td>{{item.comments}}</td>
                                </tr>
                                </tbody>
                            </table>
                        </el-tab-pane>
                        <el-tab-pane label="保养记录" name="maintain">
                            <table class="tab-table">
                                <thead>
                                <tr>
                                    <th>
                                        <el-button type="text" @click="openAdd('maintain')"><span
                                                class="icon add"></span></el-button>
                                    </th>
                                    <th>序号</th>
                                    <th>资产编号</th>
                                    <th>资产名称</th>
                                    <th>保养内容</th>
                                    <th>保养日期</th>
                                    <th>金额</th>
                                    <th>负责人</th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr v-for="(item,index) in list" :key="item.id">
                                    <td>
                                        <span class="icon edit" @click="edit(item.id,'maintain')"></span>
                                        <span class="icon del" @click="Del(item.id,'maintain')"></span>
                                    </td>
                                    <td>{{index + 1}}</td>
                                    <td>{{item.asset_num}}</td>
                                    <td>{{item.asset_name}}</td>
                                    <td>{{item.content}}</td>
                                    <td>{{item.date}}</td>
                                    <td>{{item.money}}</td>
                                    <td>{{item.leading_official}}</td>
                                </tr>
                                </tbody>
                            </table>
                        </el-tab-pane>
                    </el-tabs>
                </el-tab-pane>
            </el-tabs>
        </div>
        <el-dialog title="运输设备信息" :visible.sync="pop" size="tiny">
            <el-form :model="addForm" :label-width="width" :rules="transportRules" ref="transportRulesName">
                <el-form-item label="资产编号" prop="asset_num">
                    <el-col :span="span">
                        <el-input v-model="addForm.asset_num" size="small" placeholder="请输入资产编号"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="资产名称">
                    <el-col :span="span">
                        <el-input v-model="addForm.asset_name" size="small" placeholder="请输入资产名称"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="品牌">
                    <el-col :span="span">
                        <el-input v-model="addForm.trademark" size="small" placeholder="请输入品牌"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="车牌号" prop="number">
                    <el-col :span="span">
                        <el-input v-model="addForm.number" size="small" placeholder="请输入车牌号"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="购车时间">
                    <el-col :span="span">
                        <el-date-picker type="date" size="small" placeholder="选择日期" v-model="addForm.buy_time"
                                        style="width: 100%;"></el-date-picker>
                    </el-col>
                </el-form-item>
                <el-form-item label="存放位置" prop="location">
                    <el-col :span="span">
                        <el-input v-model="addForm.location" size="small" placeholder="请输入存放位置"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="价值">
                    <el-col :span="span">
                        <el-input v-model="addForm.value" size="small" placeholder="请输入价值"></el-input>
                    </el-col>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="pop = false">取 消</el-button>
                <el-button type="primary" @click="changeTransport()">确 定</el-button>
            </div>
        </el-dialog>
        <el-dialog title="用车记录" :visible.sync="usePop" size="tiny">
            <el-form :model="useAdd" :label-width="width" :rules="useRules" ref="useRulesName">
                <el-form-item label="用车人" prop="name">
                    <el-col :span="span">
                        <el-input v-model="useAdd.name" size="small" auto-complete="off"
                                  placeholder="请输入"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="使用日期">
                    <el-col :span="span">
                        <el-date-picker type="date" size="small" placeholder="选择日期"
                                        v-model="useAdd.use_date"
                                        style="width: 100%;"></el-date-picker>
                    </el-col>
                </el-form-item>
                <el-form-item label="归还日期">
                    <el-col :span="span">
                        <el-date-picker type="date" size="small" placeholder="选择日期"
                                        v-model="useAdd.return_date"
                                        style="width: 100%;"></el-date-picker>
                    </el-col>
                </el-form-item>
                <el-form-item label="使用目的">
                    <el-col :span="span">
                        <el-input v-model="useAdd.purpose" size="small" auto-complete="off"
                                  placeholder="请输入"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="公里数">
                    <el-col :span="span">
                        <el-input v-model="useAdd.kilometres" size="small" auto-complete="off"
                                  placeholder="请输入"></el-input>
                    </el-col>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="usePop = false">取 消</el-button>
                <el-button type="primary" @click="submit('use')">确 定</el-button>
            </div>
        </el-dialog>
        <el-dialog title="维修记录" :visible.sync="maintenancePop" size="tiny">
            <el-form :model="maintenanceAdd" :label-width="width" :rules="maintenanceRules" ref="maintenanceRulesName">
                <el-form-item label="资产编号">
                    <el-col :span="span">
                        <el-input v-model="maintenanceAdd.asset_num" size="small"
                                  auto-complete="off" disabled="disabled"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="资产名称">
                    <el-col :span="span">
                        <el-input v-model="maintenanceAdd.asset_name" size="small"
                                  auto-complete="off" disabled="disabled"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="故障发生时间">
                    <el-col :span="span">
                        <el-date-picker type="date" size="small" placeholder="选择日期"
                                        v-model="maintenanceAdd.start_time"
                                        style="width: 100%;"></el-date-picker>
                    </el-col>
                </el-form-item>
                <el-form-item label="故障现象">
                    <el-col :span="span">
                        <el-input v-model="maintenanceAdd.phenomenon" size="small"
                                  auto-complete="off" placeholder="请输入"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="维修状态">
                    <el-col :span="span">
                        <el-select v-model="maintenanceAdd.condition" placeholder="请选择">
                            <el-option v-for="item in options" :key="item" :label="item"
                                       :value="item">
                            </el-option>
                        </el-select>
                    </el-col>
                </el-form-item>
                <el-form-item label="故障解除时间">
                    <el-col :span="span">
                        <el-date-picker type="date" size="small" placeholder="选择日期"
                                        v-model="maintenanceAdd.end_time"
                                        style="width: 100%;"></el-date-picker>
                    </el-col>
                </el-form-item>
                <el-form-item label="金额">
                    <el-col :span="span">
                        <el-input v-model="maintenanceAdd.money" size="small"
                                  auto-complete="off" placeholder="请输入"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="负责人" prop="leading_official">
                    <el-col :span="span">
                        <el-input v-model="maintenanceAdd.leading_official" size="small"
                                  auto-complete="off" placeholder="请输入"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="备注">
                    <el-col :span="span">
                        <el-input v-model="maintenanceAdd.comments" size="small"
                                  auto-complete="off" placeholder="请输入"></el-input>
                    </el-col>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="maintenancePop = false">取 消</el-button>
                <el-button type="primary" @click="submit('maintenance')">确 定
                </el-button>
            </div>
        </el-dialog>
        <el-dialog title="保养记录" :visible.sync="maintainPop" size="tiny">
            <el-form :model="maintainAdd" :label-width="width" :rules="maintainRules" ref="maintainRulesName">
                <el-form-item label="资产编号">
                    <el-col :span="span">
                        <el-input v-model="maintainAdd.asset_num" size="small"
                                  auto-complete="off" disabled="disabled"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="资产名称">
                    <el-col :span="span">
                        <el-input v-model="maintainAdd.asset_name" size="small"
                                  auto-complete="off" disabled="disabled"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="保养内容">
                    <el-col :span="span">
                        <el-input v-model="maintainAdd.content" size="small"
                                  auto-complete="off" placeholder="请输入"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="保养日期">
                    <el-col :span="span">
                        <el-date-picker type="date" size="small" placeholder="选择日期"
                                        v-model="maintainAdd.date"
                                        style="width: 100%;"></el-date-picker>
                    </el-col>
                </el-form-item>
                <el-form-item label="金额">
                    <el-col :span="span">
                        <el-input v-model="maintainAdd.money" size="small"
                                  auto-complete="off" placeholder="请输入"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="负责人" prop="leading_official">
                    <el-col :span="span">
                        <el-input v-model="maintainAdd.leading_official" size="small"
                                  auto-complete="off" placeholder="请输入"></el-input>
                    </el-col>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="maintainPop = false">取 消</el-button>
                <el-button type="primary" @click="submit('maintain')">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import '../../../assets/css/info.css'

    const transport = {
        asset_name: '',
        asset_num: '',
        trademark: '',
        number: '',
        buy_time: '',
        location: '',
        value: ''
    };
    const use = {
        name: '',
        use_date: '',
        return_date: '',
        purpose: '',
        kilometres: '',
    };
    const maintenance = {
        start_time: '',
        phenomenon: '',
        condition: '',
        end_time: '',
        money: '',
        asset_num: '',
        asset_name: '',
        comments: '',
        leading_official: '',
    };
    const maintain = {
        asset_num: '',
        asset_name: '',
        content: '',
        date: '',
        money: '',
        leading_official: '',
    };
    export default {
        data() {
            return {
                activeName: 'maintenance',
                width: '200px',
                span: 15,
                options: ['代办', '完成'],
                transport: {},
                id: '',
                list: {},

                pop: false,
                addForm: _g.cloneJson(transport),
                transportRules: {
                    asset_num: [{required: true, message: "请填写资产编号", trigger: "blur"}],
                    number: [{required: true, message: "请填写车牌号", trigger: "blur"}],
                    location: [{required: true, message: "请填写存放地点", trigger: "blur"}]
                },

                usePop: false,
                useAdd: _g.cloneJson(use),
                useRules: {
                    name: [{required: true, message: "请填写使用人", trigger: "blur"}]
                },

                maintenancePop: false,
                maintenanceAdd: _g.cloneJson(maintenance),
                maintenanceRules: {
                    leading_official: [{required: true, message: "请填写负责人", trigger: "blur"}]
                },

                maintainPop: false,
                maintainAdd: _g.cloneJson(maintain),
                maintainRules: {
                    leading_official: [{required: true, message: "请填写负责人", trigger: "blur"}]
                },

            }
        },

        created() {
            this.id = this.$route.params.id;
            this.getDetail();
            this.getList('use');
        },

        methods: {
            formChange(item) {
                this.getList(item.name);
                this.activeName = 'maintenance';
            },

            handleClick(tab, event) {
                this.activeName = tab.name;
                this.getList(tab.name);
            },

            /* 获取数据 */
            getList(action) {
                let data = {pid: this.id};
                this.apiPost("Operation/transport" + action, data).then(res => {
                    this.list = res;
                });
            },
            getDetail() {
                let data = {id: this.id};
                this.apiPost("Operation/transportDetail", data).then(res => {
                    this.transport = res;
                });
            },

            /* 提交数据 */
            changeTransport() {
                this.$refs.transportRulesName.validate(valid => {
                    if (valid) {
                        this.pop = false;
                        let data = this.addForm;
                        this.apiPost("Operation/transportEdit", data).then(res => {
                            let remind = res.error ? "error" : "success";
                            _g.toastMsg(remind, res.msg);
                            if (!res.error) {
                                this.getDetail();
                            } else {
                                this.pop = true;
                            }
                        });
                    } else {
                        return false;
                    }
                });
            },
            submit(action) {
                this.$refs[action + 'RulesName'].validate(valid => {
                    if (valid) {
                        this[action + 'Pop'] = false;
                        let data = this[action + 'Add'];
                        if (action !== 'maintain' && action !== 'maintenance') {
                            data.pid = this.id;
                        }
                        this.apiPost("Operation/transport" + action + "Edit", data).then(res => {
                            let remind = res.error ? "error" : "success";
                            _g.toastMsg(remind, res.msg);
                            if (!res.error) {
                                this.getList(action);
                            } else {
                                this[action + 'Pop'] = true;
                            }
                        });
                    } else {
                        return false;
                    }
                });
            },

            /* 删除 */
            Del(id, action) {
                this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                    .then(() => {
                        let data = {id: id};
                        this.apiPost("Operation/transport" + action + "Delete", data).then(res => {
                            if (!res.error) {
                                this.$message({
                                    type: "success",
                                    message: res.msg
                                });
                            } else {
                                this.$message({
                                    type: "error",
                                    message: res.msg
                                });
                            }
                            this.getList(action);
                        });
                    })
                    .catch(() => {
                        this.$message({
                            type: "info",
                            message: "已取消删除"
                        });
                    });
            },

            /* 打开新增窗口 */
            openAdd(action) {
                this[action + 'Add'] = _g.cloneJson(eval(action));
                if (action === 'maintenance') {
                    this.maintenanceAdd.asset_num = this.transport.asset_num;
                    this.maintenanceAdd.asset_name = this.transport.asset_name;
                } else if (action === 'maintain') {
                    this.maintainAdd.asset_num = this.transport.asset_num;
                    this.maintainAdd.asset_name = this.transport.asset_name;
                }
                this[action + 'Pop'] = true;
                this.$refs[action + 'RulesName'].resetFields();
            },

            /* 打开编辑窗口 */
            edit(id, action) {
                let data = {id: id};
                this.apiPost('Operation/transport' + action + 'Detail', data).then(res => {
                    setTimeout(() => {
                        this[action + 'Pop'] = true;
                        setTimeout(() => {
                            this.$refs[action + 'RulesName'].resetFields();
                            this[action + 'Add'] = res;
                        }, 0);
                    }, 0);
                })
            },
            editTransport() {
                let data = {id: this.id};
                this.apiPost('Operation/transportDetail', data).then(res => {
                    setTimeout(() => {
                        this.pop = true;
                        setTimeout(() => {
                            this.$refs.transportRulesName.resetFields();
                            this.addForm = res;
                        }, 0);
                    }, 0);
                })
            },
        },
    }
</script>

<style scoped>
    .stuInfo {
        margin-bottom: 20px;
    }

    .headPic img {
        margin-left: 50px;
        width: 40%;
    }

    .add {
        width: 25px;
        height: 25px;
    }
</style>